<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="客户姓名" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户姓名" />
      </el-form-item>
      <el-form-item label="客户性别" prop="customerSex">
        <el-input v-model="formData.customerSex" placeholder="请输入客户性别" />
      </el-form-item>
      <el-form-item label="客户年龄" prop="customerAge">
        <el-input v-model="formData.customerAge" placeholder="请输入客户年龄" />
      </el-form-item>
      <el-form-item label="档案号" prop="recordId">
        <el-input v-model="formData.recordId" placeholder="请输入档案号" />
      </el-form-item>
      <el-form-item label="外出原因" prop="outgoingReason">
        <el-input v-model="formData.outgoingReason" placeholder="请输入外出原因" />
      </el-form-item>
      <el-form-item label="外出时间" prop="outgoingTime">
        <el-date-picker
          v-model="formData.outgoingTime"
          type="date"
          value-format="x"
          placeholder="选择外出时间"
        />
      </el-form-item>
      <el-form-item label="预计回来时间" prop="expectedReturnTime">
        <el-date-picker
          v-model="formData.expectedReturnTime"
          type="date"
          value-format="x"
          placeholder="选择预计回来时间"
        />
      </el-form-item>
      <el-form-item label="实际回来时间" prop="actualReturnTime">
        <el-date-picker
          v-model="formData.actualReturnTime"
          type="date"
          value-format="x"
          placeholder="选择实际回来时间"
        />
      </el-form-item>
      <el-form-item label="陪同人" prop="escorted">
        <el-input v-model="formData.escorted" placeholder="请输入陪同人" />
      </el-form-item>
      <el-form-item label="与老人关系" prop="relation">
        <el-input v-model="formData.relation" placeholder="请输入与老人关系" />
      </el-form-item>
      <el-form-item label="陪同人电话" prop="escortedTel">
        <el-input v-model="formData.escortedTel" placeholder="请输入陪同人电话" />
      </el-form-item>
      <el-form-item label="审批状态" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio
            v-for="dict in getStrDictOptions(DICT_TYPE.REMARK_STATUS)"

            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审批人" prop="auditPerson">
        <el-input v-model="formData.auditPerson" placeholder="请输入审批人" />
      </el-form-item>
      <el-form-item label="审批时间" prop="auditTine">
        <el-date-picker
          v-model="formData.auditTine"
          type="date"
          value-format="x"
          placeholder="选择审批时间"
        />
      </el-form-item>
      <el-form-item label="备注" prop="attention">
        <el-input v-model="formData.attention" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { OutgoingApi, OutgoingVO } from '@/api/yyzx/outgoing'

/** 外出登记 表单 */
defineOptions({ name: 'OutgoingForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  outingId:undefined,
  customerId:undefined,
  customerName: undefined,// 客户姓名
  customerSex: undefined, // 性别
  customerAge: undefined, // 年龄
  recordId: undefined, // 档案号
  outgoingReason: undefined, // 外出原因
  outgoingTime: undefined, // 外出时间
  expectedReturnTime: undefined, // 预计回来时间
  actualReturnTime: undefined, // 实际回来时间
  escorted: undefined, // 陪同人
  relation: undefined, // 与老人关系
  escortedTel: undefined, // 陪同人电话
  auditStatus: undefined,// 审批状态
  auditPerson: undefined, // 审批人
  auditTine: undefined, // 审批时间
  attention:undefined,//备注
})
const formRules = reactive({
  customerId: [{ required: true, message: '客户ID不能为空', trigger: 'blur' }],
  customerAge: [{ required: true, message: '客户年龄不能为空,且为正整数', trigger: 'blur',pattern:/^[1-9]\d*$/ }],
  customerSex: [{ required: true, message: '客户性别1为男性，2为女性', trigger: 'blur',pattern:/^[1-2]$/ }],
  recordId: [{ required: true, message: '档案号不能为空,且为正整数', trigger: 'blur',pattern:/^\d+$/ }],
  outgoingReason: [{ required: true, message: '外出原因不能为空', trigger: 'blur' }],
  escorted: [{ required: true, message: '陪同人不能为空', trigger: 'blur' }],
  relation: [{ required: true, message: '与老人关系不能为空', trigger: 'blur' }],
  escortedTel: [{ required: true, message: '陪同人电话不能为空', trigger: 'blur' }],
  auditStatus: [{ required: true, message: '审批状态不能为空', trigger: 'blur' }],
  auditPerson: [{ required: true, message: '审批人不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OutgoingApi.getOutgoing(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OutgoingVO
    if (formType.value === 'create') {
      await OutgoingApi.createOutgoing(data)
      message.success(t('common.createSuccess'))
    } else {
      await OutgoingApi.updateOutgoing(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    outingId:undefined,
    customerId:undefined,
    customerName: undefined,// 客户姓名
    customerSex: undefined, // 性别
    customerAge: undefined, // 年龄
    recordId: undefined, // 档案号
    outgoingReason: undefined, // 外出原因
    outgoingTime: undefined, // 外出时间
    expectedReturnTime: undefined, // 预计回来时间
    actualReturnTime: undefined, // 实际回来时间
    escorted: undefined, // 陪同人
    relation: undefined, // 与老人关系
    escortedTel: undefined, // 陪同人电话
    auditStatus: undefined,// 审批状态
    auditPerson: undefined, // 审批人
    auditTine: undefined, // 审批时间
    attention:undefined,//备注
  }
  formRef.value?.resetFields()
}
</script>
